/* (c)2010-2012 Andrea Caltroni (andrea.caltroni@pd.infn.it), INFN (www.infn.it)
 *
 * This file is part of the IGI Survey Portlet (from here on shortened to IGISP).
 * IGISP is free software: you can redistribute it and/or modify it 
 * under the terms of the GNU General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) any 
 * later version.
 *
 * IGISP is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with IGISP. If not, see <http://www.gnu.org/licenses/>.
 */
package it.infn.grid.liferay.survey.modules.admin.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.portlet.PortletException;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;

import org.ajm.spring.survey.modules.admin.model.Survey;
import org.ajm.spring.survey.modules.admin.service.ReportExportService;
import org.ajm.spring.survey.modules.admin.service.SurveyService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.portlet.bind.annotation.ResourceMapping;

/**
 * This controller implements the operations associated to exporting a survey
 * report.
 * 
 * @author Andrea Caltroni, andrea.caltroni@pd.infn.it, INFN
 */
@RequestMapping(value = "VIEW")
@Controller(value = "exportReportController")
public class ExportReportController {

    // ------------------------------------------------------------------------|
    // --- CONSTANTS ----------------------------------------------------------|
    // ------------------------------------------------------------------------|

    // --- CLASS VARIABLES ----------------------------------------------------|
    // ------------------------------------------------------------------------|
    // ------------------------------------------------------------------------|

    static Logger logger = Logger.getLogger(ExportReportController.class);

    // ------------------------------------------------------------------------|
    // --- INSTANCE VARIABLES -------------------------------------------------|
    // ------------------------------------------------------------------------|

    @Autowired
    @Qualifier("surveyService")
    private SurveyService surveyService;

    @Autowired
    @Qualifier("reportExportService")
    private ReportExportService reportExportService;

    // ------------------------------------------------------------------------|
    // --- CONSTRUCTORS -------------------------------------------------------|
    // ------------------------------------------------------------------------|

    // ------------------------------------------------------------------------|
    // --- CLASS METHODS ------------------------------------------------------|
    // ------------------------------------------------------------------------|

    // ------------------------------------------------------------------------|
    // --- INSTANCE METHODS ---------------------------------------------------|
    // ------------------------------------------------------------------------|

    @RequestMapping(params="action=showExportReportForm")
    public String showExportReportForm(Model model, @RequestParam("surveyID") Long surveyId) {
        
        model.addAttribute("survey", getSurveyService().findById(surveyId));
        return "reportFileDownload";
    }
    
    // ------------------------------------------------------------------------|

    /* (non-Javadoc)
     * @see javax.portlet.ResourceServingPortlet#serveResource(javax.portlet.ResourceRequest, javax.portlet.ResourceResponse)
     */
    @ResourceMapping("downloadFile")
    public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
            throws PortletException, IOException {
        
        // No checks: always involed with safe parameters
        String surveyIdStr = resourceRequest.getParameter("surveyID");
        Long surveyId = new Long(surveyIdStr);
        Survey survey = getSurveyService().findById(surveyId);
        
        String tmpFileName = "Survey-" + survey.getId() + "-CSV.txt";
        
        // build content
        byte[] bytes = getReportExportService().exportToCsvFile(survey);
        
        resourceResponse.setContentType("text/csv");
        resourceResponse.addProperty("Content-disposition", "atachment; filename=" + tmpFileName);

        OutputStream out = resourceResponse.getPortletOutputStream();
        out.write(bytes);
        out.flush();
        out.close();
    }

    // ----- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --- |
    // ---[ GETTERS AND SETTERS ]----------------------------------------------|
    // ----- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - --- |

    /**
     * @param surveyService
     *            the surveyService to set
     */
    public void setSurveyService(SurveyService surveyService) {

        this.surveyService = surveyService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @return the surveyService
     */
    public SurveyService getSurveyService() {

        return surveyService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @return the reportExportService
     */
    public ReportExportService getReportExportService() {
        return reportExportService;
    }

    // ------------------------------------------------------------------------|

    /**
     * @param reportExportService the reportExportService to set
     */
    public void setReportExportService(ReportExportService reportExportService) {
        this.reportExportService = reportExportService;
    }

    // ------------------------------------------------------------------------|

}